﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ShopB2C.Entity;
using Valor.DBUtility.MSSQLHelper;

namespace ShopB2C.SQLDataAccess
{
    public class RolesAccess 
    {
        /// <summary>
        /// 获取所有的用户组
        /// </summary>
        private const string _rolesList = "User_Level_SelectList";
        /// <summary>
        /// 增加用户组
        /// </summary>
        private const string _rolesAppend = "User_Level_Append";
        /// <summary>
        /// 删除组
        /// </summary>
        private const string _rolesRemove = "User_Level_Delete";

        public RolesAccess(string connectionstring)
        {
            SQLProcedureHelper.ConnectionString = connectionstring;
        }


        /// <summary>
        /// 获取所有的组
        /// </summary>
        /// <returns></returns>
        public List<UserLevelEntity> GetRoles()
        {
            List<UserLevelEntity> levelList = new List<UserLevelEntity>();
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader(_rolesList);
            while (reader.Read())
            {
                levelList.Add(new UserLevelEntity()
                {
                    LevelId = Convert.ToInt32(reader["LevelId"]) ,
                    MinValue = Convert.ToInt32(reader["MinValue"]) ,
                    Name = reader["Name"].ToString() ,
                    Discount = Convert.ToDecimal(reader["Discount"]) ,
                    IsDiscount = Convert.ToBoolean(reader["IsDiscount"]) ,
                    Icon = reader["Icon"].ToString()
                });
            }
            reader.Close();
            return levelList;
        }
        /// <summary>
        /// 增加用户组
        /// </summary>
        /// <param name="userLevelEntity">用户组实体类</param>
        /// <returns>被影响的行</returns>
        public int AppendRole(UserLevelEntity userLevelEntity)
        {
            int result = 0;

            SqlParameter[] parameters = 
           {
               new SqlParameter("@LevelId",SqlDbType.Int){Value = userLevelEntity.LevelId},

               new SqlParameter("@Name",SqlDbType.NVarChar,50){Value = userLevelEntity.Name},

               new SqlParameter("@MinValue",SqlDbType.Int){Value = userLevelEntity.MinValue},

               new SqlParameter("@Icon",SqlDbType.NVarChar,50){Value=userLevelEntity.Icon},

               new SqlParameter("@Discount",SqlDbType.Decimal){Value=userLevelEntity.Discount},

               new SqlParameter("@IsDiscount",SqlDbType.Bit){Value=userLevelEntity.IsDiscount}
           };

            result = SQLProcedureHelper.ExecuteNonQuery(_rolesAppend, parameters);
            return result;
        }
        /// <summary>
        /// 删除用户组
        /// </summary>
        /// <param name="levelId">用户组ID</param>
        public void RemoveRole(int levelId)
        {
            SqlParameter[] parameters = 
           {
               new SqlParameter("@LevelId",SqlDbType.Int){ Value = levelId }
           };

            SQLProcedureHelper.ExecuteNonQuery(_rolesRemove, parameters);
        }

    }
}
